<?php

namespace app\admin\command;

use app\common\library\job\TeamCountExpertLevelJob;
use app\common\library\job\TeamCountJob;
use app\common\library\TeamCount;
use app\common\library\token\driver\Redis as DriverRedis;
use app\common\library\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Log;
use think\cache\driver\Redis;
use think\Queue;

class TeamCountExpertLevelPutQueue extends Command
{
    // 休眠间隔
    protected $_sleep = 30;
    // 是否有数据正在处理
    protected $_doing = false;
    //执行任务数组
    protected $_tasks = [];
    //缓存key
    protected $cache_key = "last_team_count_expert_count_user_id";

    protected function configure()
    {
        $this->setName('TeamCountExpertLevelPutQueue')->setDescription('统计网体-各星达人的人数-放入队列');
    }

    /**
     * 休眠
     *
     * @param string $second            
     */
    public function sleep($second = "")
    {
        $second = $second ? $second : $this->_sleep;
        sleep(sleep($second)); // TP5的命令行 sleep($second) 不生效
        echo "sleep {$second} seconds ok!time:" . date('Y-m-d H:i:s') . PHP_EOL;
    }


    protected function execute(Input $input, Output $output)
    {

        cache($this->cache_key, 0);

        while (true && ! $this->_doing) {
            $this->_doing = true;
            $this->autoRun();
            $this->_doing = false;
        }
    }

    /**
     * 自动执行
     *
     * @return bool
     */
    protected function autoRun()
    {
        $this->getWorks();
        if ($this->_tasks) {

            foreach ($this->_tasks  as $user) {
                // exit(print_r($user));

                echo "pushing user:" . $user["id"] . PHP_EOL;

                // 构建任务数据
                $queueData = [
                    'user_id' => $user["id"],
                    'nickname' => $user["nickname"],
                    'mobile' => $user["mobile"],
                    'expert_level' => $user["expert_level"],
                    'is_area_manager' => $user["is_area_manager"]
                ];

                // 使用TeamCountJob类将任务推送到'TeamCountExpertLevelJob'队列
                Queue::push(TeamCountExpertLevelJob::class, $queueData, 'TeamCountExpertLevelJob');

                echo "pushed user:" . $user["id"] . PHP_EOL;

                cache($this->cache_key, $user["id"]);
            }

            $this->sleep(3);
        } else {
            echo "run all" .  PHP_EOL;
            exit();
        }
    }



    /**
     * 获取任务
     */
    protected function getWorks()
    {
        try {
            //
            $last_id = cache($this->cache_key);
            if (!$last_id) {
                $last_id = 0;
            }
            echo "last_id:" . $last_id . PHP_EOL;
            // exit($last_id . "");
            $user_list = db("user")->whereRaw("expert_level>0 or is_area_manager=1")->where("id", ">", $last_id)->field("id,nickname,mobile,expert_level,is_area_manager")->order("id", "asc")->limit(100)->select();
            return $this->_tasks = $user_list ? $user_list : [];
        } catch (Exception $e) {
            Log::write($e->getMessage());
        }
    }
}
